﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using EOSCRM.Web.Common;
using CrystalDecisions.CrystalReports.Engine;
using EOSCRM.Util;
using EOSCRM.Dao;
using EOSCRM.Web.Shared;
using ReportDocument = CrystalDecisions.CrystalReports.Engine.ReportDocument;

namespace EOSCRM.Web.Forms.DongHo.BaoCao
{
    public partial class DanhSachDongHoTheoDuong : Authentication
   {
        private readonly DuongPhoDao dpDao = new DuongPhoDao();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                

                if (!Page.IsPostBack)
                {
                    LoadReferences();
                }
                else 
                {
                    var dt = (DataTable)Session[SessionKey.DH_BAOCAO_DANHSACHDONGHOTHEODUONG];
                    Report(dt);
                }
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }

        private void LoadReferences()
        {
            txtNguoiLap.Text = LoginInfo.NHANVIEN.HOTEN;

            var currentDate = DateTime.Now;
            var firstDayInMonth = new DateTime(currentDate.Year, currentDate.Month, 1);
            txtTuNgay.Text = firstDayInMonth.ToString("dd/MM/yyyy");
            txtDenNgay.Text = currentDate.ToString("dd/MM/yyyy");
        }

        #region Startup script registeration
        private void SetControlValue(string id, string value)
        {
            var master = (EOS)Page.Master;
            if (master != null) master.SetControlValue(id, value);
        }

        private void HideDialog(string divId)
        {
            var master = (EOS) Page.Master;
            if (master != null) master.HideDialog(divId);
        }

        private void UnblockDialog(string divId)
        {
            var master = (EOS) Page.Master;
            if (master != null) master.UnblockDialog(divId);
        }

        private void CloseWaitingDialog()
        {
            var master = (EOS) Page.Master;
            if (master != null) master.CloseWaitingDialog();
        }
        #endregion

        protected void btnBaoCao_Click(object sender, EventArgs e)
        {
            var TuNgay = DateTimeUtil.GetVietNamDate(txtTuNgay.Text.Trim());
            var DenNgay = DateTimeUtil.GetVietNamDate(txtDenNgay.Text.Trim());

            var ds =
                new ReportClass().ThongKeDongHoTheoDuong(TuNgay, DenNgay, txtMADP.Text.Trim());
            if (ds == null || ds.Tables.Count == 0) { CloseWaitingDialog(); return; }

            Report(ds.Tables[0]);
            CloseWaitingDialog();
        }

        private void Report(DataTable dt)
        {
            if (dt == null)
                return;

            var rp = (ReportDocument)Session[Constants.REPORT_FREE_MEM];
            if (rp != null)
            {
                try
                {
                    rp.Close();
                    rp.Dispose();
                    GC.Collect();
                }
                // ReSharper disable EmptyGeneralCatchClause
                catch
                // ReSharper restore EmptyGeneralCatchClause
                {

                }
            }

            rp = new ReportDocument();

            SetTitle(rp);

            rp.SetDataSource(dt);
            rpViewer.ReportSource = rp;
            rpViewer.DataBind();

            divreport.Visible = true;
            upnlReport.Update();

            Session[SessionKey.DH_BAOCAO_DANHSACHDONGHOTHEODUONG] = dt;
            Session[Constants.REPORT_FREE_MEM] = rp;
        }

        private void SetTitle(ReportDocument rp)
        {
            var path = Server.MapPath("../../../Reports/KiemDinhDongHo/DanhSachDongHoTheoDuong.rpt");
            rp.Load(path);
            string[] controls = { "txtNgayThangNam", "txtNguoiLap","txtTitle" };
            string[] values = { ReportFuntions.GetNgayThangNam(), txtNguoiLap.Text.Trim(),"THÔNG TIN KHÁCH HÀNG THEO ĐƯỜNG" };
            ReportFuntions._SetTitle(ref rp, controls, values);
            var txtTuNgay1 = rp.ReportDefinition.ReportObjects["txtTuNgay"] as TextObject;
            if (txtTuNgay1 != null)
                txtTuNgay1.Text = "Từ ngày " + txtTuNgay.Text.Trim() + " đến ngày " + txtDenNgay.Text.Trim();
        }

        #region Đường phố
        protected void btnFilterDP_Click(object sender, EventArgs e)
        {
            BindDuongPho();
            CloseWaitingDialog();
        }

        private void BindDuongPho()
        {
            var list = dpDao.GetList("%", txtFilterDP.Text.Trim());
            gvDuongPho.DataSource = list;
            gvDuongPho.PagerInforText = list.Count.ToString();
            gvDuongPho.DataBind();
        }

        protected void btnBrowseDP_Click(object sender, EventArgs e)
        {
            BindDuongPho();
            upnlDuongPho.Update();
            UnblockDialog("divDuongPho");
        }

        protected void gvDuongPho_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                var id = e.CommandArgument.ToString();

                switch (e.CommandName)
                {
                    case "SelectMADP":
                        var dp = dpDao.Get(id);
                        if (dp != null)
                        {
                            SetControlValue(txtMADP.ClientID, dp.MADP);
                            upnlBaoCao.Update();

                            HideDialog("divDuongPho");
                            CloseWaitingDialog();

                            txtMADP.Focus();
                        }

                        break;

                }
            }
            catch (Exception ex)
            {
                DoError(new Message(MessageConstants.E_EXCEPTION, MessageType.Error, ex.Message, ex.StackTrace));
            }
        }

        #endregion
    }
}